package com.gyc.datastructure.linklist;

/**
 * @author gyc
 * @date 2017/12/26
 * 对链表进行插入数据，删除指定位置数据，排序，打印，删除重复数据
 */
public class MyLinkedList {
    Node head=null;

    /**
     * 向链表插入数据
     * @param value 数据内容
     */
    public void addNode(int value){
        Node newnode=new Node(value);
        if(head==null){
            head=newnode;
            return;
        }
        Node temp=head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=newnode;
    }

    /**
     * 返回链表长度
     * @return
     */
    public int length(){
        int length=0;
        Node temp=head;
        while (temp!=null){
            length++;
            temp=temp.next;
        }
        return length;
    }

    /**
     * 删除节点
     * @param index 删除第index个节点
     * @return
     */
    public boolean deleteNode(int index){
        if(index<1 || index>length()){
            return false;
        }
        if(index==1){
            head=head.next;
            return true;
        }
        int i=2;
        Node before=head;
        Node after=before.next;
        while (after!=null){
            if(i==index){
                before.next=after.next;
                return true;
            }
            before=after;
            after=after.next;
            i++;
        }
        return true;
    }

    /**
     * 对链表进行排序（冒泡）
     * @return
     */
    public Node orderNode(){
        int temp=0;
        Node before=head;
        while (before.next!=null){
            Node after=before.next;
            while (after!=null){
                if(before.value>after.value){
                    temp=before.value;
                    before.value=after.value;
                    after.value=temp;
                }
                after=after.next;
            }
            before=before.next;
        }
        return head;
    }

    /**
     * 打印链表
     */
    public void printNode(){
        Node print=head;
        while (print!=null){
            System.out.println(print.value);
            print=print.next;
        }
    }


    /**
     * 删除重复数据
     */
    public void deleteRepeat(){
        Node before=head;
        while (before!=null){
            Node a=before;
            Node after=before.next;
            while (after!=null){
                if(before.value==after.value){
                    a.next=after.next;
                }
                a=after;
                after=after.next;
            }
            before=before.next;
        }

    }

    /**
     * 将链表数据反转
     * @param node
     * @return
     */
    public void ReversalNode(Node node){
        Node reversalhead=node;
        Node b=node;
        Node c=null;
        while (b!=null){
            Node pnext=b.next;
            if(pnext==null){
                reversalhead=b;
            }
            b.next=c;
            c=b;
            b=pnext;
        }
        this.head= reversalhead;
    }

    /**
     * 反向打印链表（递归思想）
     * @param node
     */
    public void ReversalPrint(Node node){
        if(node!=null){
            ReversalPrint(node.next);
            System.out.println(node.value);
        }
    }


    public static void main(String[] args){
        MyLinkedList linkedList=new MyLinkedList();
        linkedList.addNode(3);
        linkedList.addNode(1);
        linkedList.addNode(1);
        linkedList.addNode(5);
        linkedList.addNode(5);
        linkedList.addNode(3);
        //linkedList.deleteNode(1);
        System.out.println("长度:"+linkedList.length());
        linkedList.deleteRepeat();
        linkedList.printNode();

        linkedList.orderNode();
        System.out.println("排序后：");
        linkedList.ReversalNode(linkedList.head);
        linkedList.printNode();
    }

}
